//
//  TSCMyCellCommon.m
//  Tsc_IOS
//
//  Created by Rio on 15-10-08.
//  Copyright (c) 2015年 idream. All rights reserved.
//


#import "TSCMyCellCommon.h"
#import "TSCSDK.h"

@implementation TSCMyCellCommon

- (id) init {
    self = [super init];
    if (self) {
        [self initialMyCellCommon];
    }
    return self;
}

// 注：一定要实现这个方法，因为在UITableView的delegate方法中调用的是这个初始化方法
- (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initialMyCellCommon];
    }
    return self;
}

// 自定义初始化方法
- (void) initialMyCellCommon {
    
    // == 创建各UI元素
    
    // 初始化padding
    _padding = 5.0f;
    
    // 创建ivPic
    _ivPic = [[UIImageView alloc]init];
    [self addSubview:_ivPic];
    
    // 创建lb1、lb2
    _lb1 = [[UILabel alloc]init];
    _lb1.font = [TSCMyUIUtil getSystemDefaultFont];
    [self addSubview:_lb1];
    
    _lb2 = [[UILabel alloc]init];
    _lb2.font = [TSCMyUIUtil getSystemDefaultFont];
    [self addSubview:_lb2];
    
    // 创建lbTime
    _lbTime = [[UILabel alloc]init];
    _lbTime.font = [TSCMyUIUtil getSystemDefaultFont];
    _lbTime.textAlignment =  NSTextAlignmentCenter;
    _lbTime.numberOfLines = 0; // 行高无限制
    [self addSubview:_lbTime];
    
    // 创建lbRb、ivRb
    _lbRb = [[UILabel alloc]init];
    _lbRb.font = [TSCMyUIUtil getSystemDefaultFont];
    _lbRb.textAlignment =  NSTextAlignmentCenter;
    [self addSubview:_lbRb];
    _lbRb.hidden = YES; // 默认隐藏
    
    UIView * ivRbWrapper = [[UIView alloc]init]; // ivRb的包装器，用来给ivRb加入padding
    //    ivRbWrapper.backgroundColor = [UIColor yellowColor]; // TODO delete
    [self addSubview:ivRbWrapper];
    
    _ivRb = [[UIImageView alloc]init];
    [self addSubview:_ivRb];
    _ivRb.hidden = YES; // 默认隐藏
    
    // 创建ivRbGp1/2/3
    _ivRbGp1 = [[UIImageView alloc]init];
    [self addSubview:_ivRbGp1];
    _ivRbGp1.hidden = YES; // 默认隐藏
    
    _ivRbGp2 = [[UIImageView alloc]init];
    [self addSubview:_ivRbGp2];
    _ivRbGp2.hidden = YES; // 默认隐藏
    
    _ivRbGp3 = [[UIImageView alloc]init];
    [self addSubview:_ivRbGp3];
    _ivRbGp3.hidden = YES; // 默认隐藏
    
    // == 设置各UI元素约束
    
    // ivPic
    _ivPic.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-%f-[_ivPic]", _padding] options:0 metrics:nil views:NSDictionaryOfVariableBindings(_ivPic)]];
    [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-%f-[_ivPic]-%f-|", _padding, _padding] options:0 metrics:nil views:NSDictionaryOfVariableBindings(_ivPic)]];
    [NSLayoutConstraint constraintWithItem:_ivPic attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:_ivPic attribute:NSLayoutAttributeHeight multiplier:1.0f constant:0.0f].active = YES;
    // lb1、lb2
    _lb1.translatesAutoresizingMaskIntoConstraints = NO;
    _lb2.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:[_ivPic]-%f-[_lb1]", _padding] options:0 metrics:nil views:NSDictionaryOfVariableBindings(_ivPic, _lb1)]];
    [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:[_ivPic]-%f-[_lb2]", _padding] options:0 metrics:nil views:NSDictionaryOfVariableBindings(_ivPic, _lb2)]];
    [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|[_lb1(==_lb2)][_lb2]|"] options:0 metrics:nil views:NSDictionaryOfVariableBindings(_lb1, _lb2)]];
    // lbTime
    _lbTime.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:[_lb1][_lbTime(==150)]|"] options:0 metrics:nil views:NSDictionaryOfVariableBindings(_lb1, _lbTime)]];
    [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:[_lb2][_lbTime]|"] options:0 metrics:nil views:NSDictionaryOfVariableBindings(_lb2, _lbTime)]];
    [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|[_lbTime]|"] options:0 metrics:nil views:NSDictionaryOfVariableBindings(_lbTime)]];
    // lbRb、ivRbWrapper
    _lbRb.translatesAutoresizingMaskIntoConstraints = NO;
    ivRbWrapper.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:[_lb1][_lbRb(==150)]|"] options:0 metrics:nil views:NSDictionaryOfVariableBindings(_lb1, _lbRb)]];
    [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:[_lb1][ivRbWrapper(==_lbRb)]|"] options:0 metrics:nil views:NSDictionaryOfVariableBindings(_lb1, ivRbWrapper, _lbRb)]];
    [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|[_lbRb(==ivRbWrapper)][ivRbWrapper]|"] options:0 metrics:nil views:NSDictionaryOfVariableBindings(_lbRb, ivRbWrapper)]];
    // ivRb
    _ivRb.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint constraintWithItem:_ivRb attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:ivRbWrapper attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f].active = YES;
    [NSLayoutConstraint constraintWithItem:_ivRb attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:ivRbWrapper attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f].active = YES;
    [NSLayoutConstraint constraintWithItem:_ivRb attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:_ivRb attribute:NSLayoutAttributeHeight multiplier:1.0f constant:0.0f].active = YES;
    [NSLayoutConstraint constraintWithItem:_ivRb attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:ivRbWrapper attribute:NSLayoutAttributeHeight multiplier:1.0f constant:-_padding * 2].active = YES;
    // ivRbGp2
    _ivRbGp2.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint constraintWithItem:_ivRbGp2 attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:ivRbWrapper attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f].active = YES;
    [NSLayoutConstraint constraintWithItem:_ivRbGp2 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:ivRbWrapper attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f].active = YES;
    [NSLayoutConstraint constraintWithItem:_ivRbGp2 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:_ivRbGp2 attribute:NSLayoutAttributeHeight multiplier:1.0f constant:0.0f].active = YES;
    [NSLayoutConstraint constraintWithItem:_ivRbGp2 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:ivRbWrapper attribute:NSLayoutAttributeHeight multiplier:1.0f constant:-_padding * 2].active = YES;
    // ivRbGp1/3
    _ivRbGp1.translatesAutoresizingMaskIntoConstraints = NO;
    _ivRbGp3.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:[_ivRbGp1(==_ivRbGp2)]-%f-[_ivRbGp2]-%f-[_ivRbGp3(==_ivRbGp1)]", _padding, _padding] options:0 metrics:nil views:NSDictionaryOfVariableBindings(_ivRbGp1, _ivRbGp2, _ivRbGp3)]];
    [NSLayoutConstraint constraintWithItem:_ivRbGp1 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:_ivRbGp2 attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f].active = YES;
    [NSLayoutConstraint constraintWithItem:_ivRbGp3 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:_ivRbGp2 attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f].active = YES;
    [NSLayoutConstraint constraintWithItem:_ivRbGp1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:_ivRbGp2 attribute:NSLayoutAttributeHeight multiplier:1.0f constant:0.0f].active = YES;
    [NSLayoutConstraint constraintWithItem:_ivRbGp3 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:_ivRbGp2 attribute:NSLayoutAttributeHeight multiplier:1.0f constant:0.0f].active = YES;
    
}

@end
